package com.yunche.yunchebusiness.base

import android.content.Context
import android.content.Intent
import android.content.pm.ActivityInfo
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.view.MenuItem
import android.view.View
import android.view.Window
import android.view.WindowManager
import android.view.inputmethod.InputMethodManager
import com.yunche.yunchebusiness.utils.ToastyUtils

/**
 * Create by Crcker on 2018/12/20 11:04
 * Email: Crcker@163.com
 */


abstract class BaseActivity : AppCompatActivity() {
    //是否全屏
    var isFullScreen = false
    var mContext: Context? = null
    private var currentTime: Long = 0
    private var secondTime: Long = 0

    enum class ScreenOrientation {
        LANDSCAPE, PORTRAIT
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        mContext = this
        changeScreenOrientation(ScreenOrientation.PORTRAIT)
        setContentView(setLayout())
        ActivityManager.pushActivity(this)
        initView()
        initData()
        initEvent()
    }

    /**
     *  @author crcker
     *  @time 2018/9/28  6:00 PM
     *  @parmar
     *  @describe  对toolbar返回键的统一处理  ActivityManager退出所有栈
     */
    override fun onOptionsItemSelected(item: MenuItem?): Boolean {
        if (item?.itemId == android.R.id.home) {
            finish()
        }
        return super.onOptionsItemSelected(item)
    }

    fun exitApp() {
        ToastyUtils.showInfo("再按一次返回键退出")
        if (currentTime == 0L) {
            currentTime = System.currentTimeMillis()
            return
        } else {
            secondTime = System.currentTimeMillis()
        }

        if ((secondTime - currentTime) / 1000 < 2) {
            ActivityManager.exitApp()
        } else {
            currentTime = System.currentTimeMillis()
        }
    }


    override fun onDestroy() {
        super.onDestroy()
        ActivityManager.popActivity(this)
    }

    /**
     *  @author Crcker
     *  @parmar
     *  @describe  设置屏幕的方向，在设置layout之前调用。不设置则根据方向传感器旋转，手机需开启方向旋转
     */
    open fun changeScreenOrientation(orientation: ScreenOrientation) {
        requestedOrientation = if (orientation == ScreenOrientation.LANDSCAPE) {
            ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE
        } else {
            ActivityInfo.SCREEN_ORIENTATION_PORTRAIT
        }
    }

    open fun hideInput(view:View) {
        var imm = getSystemService(Context.INPUT_METHOD_SERVICE) as InputMethodManager
        imm.hideSoftInputFromWindow(view.windowToken, 0)
    }


    /**
     *  @author crcker
     *  @parmar
     *  @describe  设置全屏 需子类在setlayout方法return之前调用
     */
    open fun setFullScreen() {
        isFullScreen = true
        requestWindowFeature(Window.FEATURE_NO_TITLE)
        window.setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN)
        val actionBar = supportActionBar
        actionBar?.hide()
    }


    //跳转Activity
    open fun openActivity(clazz: Class<*>) {
        startActivity(Intent(mContext, clazz))
    }

    //携带数据 跳转
    open fun openActivityWithBundle(clz: Class<*>, bundle: Bundle?) {
        val intent = Intent(mContext, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivity(intent)
    }

    //带返回值的跳转
    open fun openActivityForResult(clz: Class<*>, bundle: Bundle?, requestCode: Int) {
        val intent = Intent(mContext, clz)
        if (bundle != null) {
            intent.putExtras(bundle)
        }
        startActivityForResult(intent, requestCode)
    }


    protected abstract fun setLayout(): Int
    protected abstract fun initView()
    protected abstract fun initData()
    protected abstract fun initEvent()
}